import 'package:flutter_menu/gen/assets.gen.dart';
import 'package:get/get.dart';

import '../../res/app_design.dart';

class SelectNumDialog extends StatefulWidget {
  final Function(int) onSubmit;
  final int? select;
  final int maxCount;

  SelectNumDialog(this.onSubmit,{this.select,this.maxCount = 30});

  @override
  _SelectNumState createState() => _SelectNumState();
}

class _SelectNumState extends State<SelectNumDialog> {
  int? select;


  @override
  void initState() {
    super.initState();
    select = widget.select;
  }

  @override
  Widget build(BuildContext context) {
    return Dialog(
      insetPadding: EdgeInsets.zero,
      backgroundColor: Colors.transparent,
      child: Stack(
        children: [
          Positioned(bottom: 0, left: 0, child: _content()),
          Positioned(right: 0, top: 0, child: _close()),
        ],
      ).size(width: 611.w, height: 690.w),
    );
  }

  _close() {
    return GestureDetector(
        onTap: () {
          Get.back();
        },
        child: MyAssets.images.icDgClose.image(width: 50, height: 50));
  }

  _content() {
    return Container(
      width: 586.w,
      height: 665.w,
      decoration: BoxDecoration(
          color: AppColors.white, borderRadius: BorderRadius.all(Radius.circular(10.w))),
      child: Column(
        children: [
          Text(
            "select_oder_num".tr,
            style: TextStyle(fontSize: AppDimen.sp_23),
          ).center().size(height: 76.w),
          _divider(),
          _selectNum().expanded(),
          _divider(),
          _submit()
        ],
      ),
    );
  }

  _submit() {
    var index = select;
    var submit = index == null ? null :(){
      widget.onSubmit.call(index);
    };
    return TextButton(
            style: AppStyle.app_btn_light,
            onPressed:submit,
            child: Text(
              "start_order".tr,
            ))
        .size(width: double.infinity, height: 54.w)
        .marginOnly(top: 23.w, bottom: 34.w, left: 12.w, right: 12.w);
  }

  Widget _selectNum() {
    return GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 5, childAspectRatio: 1.89, crossAxisSpacing: 8.w, mainAxisSpacing: 8.w),
      itemCount: widget.maxCount,
      padding: EdgeInsets.all(8.w),
      itemBuilder: (context, index) {
        var bgColor = index == select ? AppColors.e3a671 : Colors.white;
        var txColor = index == select ? AppColors.white : AppColors.text_4F;

        return GestureDetector(
          onTap: () {
            setState(() {
              select = index;
            });
          },
          child: Card(
            elevation: 4,
            color: bgColor,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(6.w), // 设置圆角
            ),
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Text(
                '${index+1}',
                style: TextStyle(fontSize: 29.sp, color: txColor),
              ).center(),
            ),
          ),
        );
      },
    );
  }

  _divider() {
    return Container(
      color: const Color(0xffE2E2E2),
      height: 1.w,
    );
  }
}
